﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibraryManagementSystem
{
    class BookManageClass: ViewClassCheckInputValue, interfaceNodeClass
    {

        public BookManageClass()
        {
        }

        public void  Running(Library library) 
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine("欢迎进入图书管理系统");
                Console.WriteLine("请输入您需要的业务号码");
                Console.WriteLine("1.添加新图书");
                Console.WriteLine("2.删除现有图书");
                Console.WriteLine("3.查询特定图书信息");
                Console.WriteLine("4.退出");
                string num = InputString();

                switch (num) 
                {

                    //▪ 添加新图书（包含书名、作者、库存数量等）。
                    case "1":
                        AddBook(library);
                        break;
                    //▪ 删除现有图书。
                    case "2":
                        BookDelete(library);
                        break;
                    //▪ 查询图书信息（支持按书名、作者查询）。
                    case "3":
                        BookByNameRoAuthor(library);
                        break;
                    case "4":
                        PromptWait("退出图书管理系统");
                        return;
                    default:
                        PromptWait("指令错误");
                        break;
                }

            }
        }

        private void AddBook( Library library) 
        {
            while (true) 
            {
               Console.Clear();
                Console.WriteLine("-------------加入新书籍--------------");
               String BookName = InputString("请输入书名");
               String BookAuthor = InputString("请输入作者名");
               int  BookAmount = InputInt("请输入书本数量");


                string A= library.Add(BookName, BookAuthor, BookAmount);
                PromptWait(A);

                string I = InputString("还要继续添加书本吗？（Y/N）").ToUpper();
                if (I == "N" ) 
                {
                    PromptWait("已退出");
                    return;
                }
                

            }
        }

        private void BookDelete(Library library) 
        {
            Console.Clear();
            Console.WriteLine("-------------删除书籍--------------");
            String BookName = InputString("请输入书名");
            String BookAuthor = InputString("请输入作者名");

            string result = library.DeleteBook(BookName,BookAuthor);

            PromptWait(result);
            string A = InputString("还要继续删除书籍吗？（Y/N）").ToUpper();
            if (A == "N") 
            {
                PromptWait("已退出");
                Console.Clear();
                return;
            }


        }

        private void BookByNameRoAuthor(Library library)
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine("-------------查找书籍--------------");
                Console.WriteLine("您要以何种方式查找？（输入数字）");
                Console.WriteLine("1.按照书名查找");
                Console.WriteLine("2.按照作者名字查找");
                Console.WriteLine("3.结束查找");

                string A = InputString();
                string finValue ;
                switch (A)
                {
                    case "1":
                        finValue = InputString("请输入书名");
                        ForEachBookList( library.FindBook(finValue, FindType.BookName));
                        break;
                    case "2":
                        finValue = InputString("请输入作者名");
                        ForEachBookList (library.FindBook(finValue, FindType.Author));
                        break;
                    case "3":
                        PromptWait("退出查询");
                        Console.Clear();
                        return;
                    default:
                        PromptWait("指令错误");
                        break;
                }
                

                


            }
        }

        private void ForEachBookList( List<BookClass> BKC ) 
        {
            if (BKC.Count < 1) { PromptWait("查无此书"); }
            else 
            {
                foreach (BookClass i in BKC ) 
                {
                    Console.WriteLine($" {i.BookName}   作者：{i.BookAuthor}    现存数量：{i.BookAmount} ");
                }
                PromptWait("查找结果如上，点击任意键退出");
            }
        }



    }
}
